{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-08-09T17:45:26.978400201Z",
     "start_time": "2023-08-09T17:45:26.029092581Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from ecgdetectors import Detectors\n",
    "import sys\n",
    "from MyEDFImports import load_data_one_file, get_edf_filenames, import_ecg, load_all_data, load_all_labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting EDF parameters from /home/tadeusz/Desktop/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/CP229110.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/tadeusz/Desktop/Tadeusz/mgr_sleep_states/MyEDFImports.py:39: RuntimeWarning: Channel names are not unique, found duplicates for: {'CHIN EMG'}. Applying running numbers for duplicates.\n",
      "  raw = mne.io.read_raw_edf(path + \"//\" + name)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /home/tadeusz/Desktop/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/WD224010.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /home/tadeusz/Desktop/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/TK221110.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /home/tadeusz/Desktop/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/VP214110.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /home/tadeusz/Desktop/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/CN223100.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/tadeusz/Desktop/Tadeusz/mgr_sleep_states/MyEDFImports.py:39: RuntimeWarning: Channel names are not unique, found duplicates for: {'CHIN EMG'}. Applying running numbers for duplicates.\n",
      "  raw = mne.io.read_raw_edf(path + \"//\" + name)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /home/tadeusz/Desktop/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/LM230010.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /home/tadeusz/Desktop/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/VC209100.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /home/tadeusz/Desktop/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/LA216100.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /home/tadeusz/Desktop/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/DG220020.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /home/tadeusz/Desktop/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/DO223050.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "Extracting EDF parameters from /home/tadeusz/Desktop/Tadeusz/mgr_sleep_states/Jean-Pol_repaired_headers/CX230050.edf...\n",
      "EDF file detected\n",
      "Setting channel info structure...\n",
      "Creating raw.info structure...\n",
      "NOTE: pick_channels() is a legacy function. New code should use inst.pick(...).\n",
      "file with 2007 windows\n",
      "file with 1777 windows\n",
      "file with 1599 windows\n",
      "file with 1725 windows\n",
      "file with 1561 windows\n",
      "file with 1724 windows\n",
      "file with 1843 windows\n",
      "file with 1633 windows\n",
      "file with 1775 windows\n",
      "file with 1806 windows\n",
      "file with 1798 windows\n"
     ]
    }
   ],
   "source": [
    "all_data = load_all_data()\n",
    "all_labels = load_all_labels()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T17:45:34.658594052Z",
     "start_time": "2023-08-09T17:45:26.980071147Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "class ECG_20:\n",
    "    def __init__(self, label, ecg, qrs):\n",
    "        self.label = label,\n",
    "        self.ecg = ecg,\n",
    "        self.qrs = qrs\n",
    "detectors = Detectors(500)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T17:45:34.662529326Z",
     "start_time": "2023-08-09T17:45:34.660753909Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "from MyEDFImports import remove_ecg_artifacts, three_stages_transform"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T17:46:50.670571566Z",
     "start_time": "2023-08-09T17:46:50.664491155Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19248\n",
      "13604\n"
     ]
    }
   ],
   "source": [
    "print(len(all_data))\n",
    "filtered_data, filter_labels = remove_ecg_artifacts(all_data, all_labels)\n",
    "print(len(filtered_data))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T17:47:26.805073796Z",
     "start_time": "2023-08-09T17:47:26.237265135Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counter({1: 8856, 2: 2512, 0: 2236})\n",
      "Counter({1: 12256, 0: 3519, 2: 3473})\n"
     ]
    }
   ],
   "source": [
    "# classes after filter\n",
    "\n",
    "from collections import Counter\n",
    "counted_filtered_labels = Counter(filter_labels)\n",
    "print(counted_filtered_labels)\n",
    "print(Counter(three_stages_transform(all_labels)))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T17:47:28.006166458Z",
     "start_time": "2023-08-09T17:47:27.999556094Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "sum(counted_filtered_labels.values())"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [],
   "source": [
    "r_peaks = []\n",
    "for i,x in enumerate(filtered_data):\n",
    "    if i+1 % 100 == 0:\n",
    "        print(i)\n",
    "    p = detectors.engzee_detector(x)\n",
    "    r_peaks.append(p)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T18:07:19.639385326Z",
     "start_time": "2023-08-09T17:51:45.630380484Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "outputs": [
    {
     "data": {
      "text/plain": "<matplotlib.patches.StepPatch at 0x7f6912cda9a0>"
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": "<Figure size 640x480 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAjEAAAGdCAYAAADjWSL8AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAleElEQVR4nO3df1BV953/8deVX0ECR9DAFSUObahrA3G7uIuw2cWNSnVKyI5/mJQMY2ddbWqiy4jjxuaPkJ0OuH4nahuaxJhMNdUU/9jYzWRbKk4SUkdRdMMEfySTTNgELVdM9nL5EXKvxc/+0a9n94pRrpocPvB8zNwZOefN5XNOT8pzDveCzxhjBAAAYJlJXi8AAADgRhAxAADASkQMAACwEhEDAACsRMQAAAArETEAAMBKRAwAALASEQMAAKwU7/UCviqXLl3SH/7wB6Wmpsrn83m9HAAAMArGGPX39ys7O1uTJl37Xsu4jZg//OEPysnJ8XoZAADgBnR1dWnmzJnXnBm3EZOamirpTychLS3N49UAAIDR6OvrU05Ojvt9/FrGbcRc/hFSWloaEQMAgGVG81IQXtgLAACsRMQAAAArETEAAMBKRAwAALASEQMAAKxExAAAACsRMQAAwEpEDAAAsBIRAwAArETEAAAAKxExAADASkQMAACwEhEDAACsNG7/ijWA8e1c75CCgxGvlxGT9JREzZiS7PUygHGDiAFgnXO9Q1r0dIuGLg57vZSYJCfE6WBNKSED3CJEDADrBAcjGro4rO0P/rnuyrzd6+WMyoc9A6re167gYISIAW4RIgaAte7KvF35MxyvlwHAI7ywFwAAWImIAQAAViJiAACAlWKKmNraWvl8vqiH3+939xtjVFtbq+zsbCUnJ2vBggU6depU1HOEw2GtXbtW06ZNU0pKiioqKnT27NmomWAwqKqqKjmOI8dxVFVVpd7e3hs/SgAAMO7EfCfm7rvvVnd3t/vo6Ohw923ZskVbt25VQ0OD2tra5Pf7tXjxYvX397sz1dXV2r9/vx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AMBKRAwAALASEQMAAKxExAAAACsRMQAAwEpEDAAAsBIRAwAArETEAAAAKxExAADASkQMAACwEhEDAACsRMQAAAArETEAAMBKRAwAALASEQMAAKxExAAAACsRMQAAwEpEDAAAsBIRAwAArETEAAAAKxExAADASkQMAACwEhEDAACsRMQAAAArETEAAMBKRAwAALASEQMAAKxExAAAACsRMQAAwEpEDAAAsBIRAwAArETEAAAAKxExAADASkQMAACwEhEDAACsRMQAAAArETEAAMBKRAwAALASEQMAAKxExAAAACsRMQAAwEpEDAAAsBIRAwAArETEAAAAKxExAADASkQMAACwEhEDAACsRMQAAAArETEAAMBKRAwAALASEQMAAKxExAAAACsRMQAAwEpEDAAAsBIRAwAArETEAAAAKxExAADASkQMAACw0k1FTH19vXw+n6qrq91txhjV1tYqOztbycnJWrBggU6dOhX1eeFwWGvXrtW0adOUkpKiiooKnT17NmomGAyqqqpKjuPIcRxVVVWpt7f3ZpYLAADGkRuOmLa2Nr3wwgu65557orZv2bJFW7duVUNDg9ra2uT3+7V48WL19/e7M9XV1dq/f78aGxt16NAhDQwMqLy8XMPDw+5MZWWl2tvb1dTUpKamJrW3t6uqqupGlwsAAMaZG4qYgYEBPfzww9q5c6fS09Pd7cYYbd++XU888YSWLVum/Px87d69W59//rleeeUVSVIoFNJLL72kp59+WosWLdJ3vvMd7dmzRx0dHTp48KAk6cyZM2pqatKLL76o4uJiFRcXa+fOnXr99df1/vvv34LDBgAAtruhiHn00Uf1ve99T4sWLYra3tnZqUAgoLKyMndbUlKSSktLdfjwYUnSiRMndPHixaiZ7Oxs5efnuzNHjhyR4zgqKipyZ+bPny/HcdyZK4XDYfX19UU9AADA+BUf6yc0NjbqP//zP9XW1jZiXyAQkCRlZWVFbc/KytLHH3/sziQmJkbdwbk8c/nzA4GAMjMzRzx/ZmamO3Ol+vp6PfXUU7EeDgAAsFRMd2K6urr0T//0T9qzZ49uu+22L53z+XxRHxtjRmy70pUzV5u/1vNs2rRJoVDIfXR1dV3z6wEAALvFFDEnT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ozM1N///d/r/fffz9qZqJfR6M5RxP5Onruued0zz33uL/Qrri4WL/97W/d/TZfP0RMDPbt26fq6mo98cQTeuedd/Q3f/M3Wrp0qT755BOvlzZm3H333eru7nYfHR0dXi/JU4ODg5o7d64aGhquun/Lli3aunWrGhoa1NbWJr/fr8WLF6u/v/9rXql3rneOJGnJkiVR19VvfvObr3GF3mppadGjjz6q1tZWNTc3649//KPKyso0ODjozkz062g050iauNfRzJkztXnzZh0/flzHjx/XfffdpwceeMANFauvH4NR+6u/+ivzyCOPRG37sz/7M/P44497tKKx5cknnzRz5871ehljliSzf/9+9+NLly4Zv99vNm/e7G774osvjOM45vnnn/dghd678hwZY8yKFSvMAw884Ml6xqKenh4jybS0tBhjuI6u5spzZAzX0ZXS09PNiy++aP31w52YUYpEIjpx4oTKysqitpeVlenw4cMerWrs+eCDD5Sdna3c3Fw99NBD+uijj7xe0pjV2dmpQCAQdU0lJSWptLSUa+oKb731ljIzM/Wtb31Lq1atUk9Pj9dL8kwoFJIkZWRkSOI6uporz9FlXEfS8PCwGhsbNTg4qOLiYuuvHyJmlD799FMNDw+P+OOTWVlZI/5I5URVVFSkl19+Wb/73e+0c+dOBQIBlZSU6LPPPvN6aWPS5euGa+rali5dqr179+qNN97Q008/rba2Nt13330Kh8NeL+1rZ4zR+vXrde+99yo/P18S19GVrnaOJK6jjo4O3X777UpKStIjjzyi/fv369vf/rb118+Y/rMDY5HP54v62BgzYttEtXTpUvffBQUFKi4u1je/+U3t3r1b69ev93BlYxvX1LU9+OCD7r/z8/M1b948zZo1S//xH/+hZcuWebiyr99jjz2md999V4cOHRqxj+voT77sHE3062j27Nlqb29Xb2+v/u3f/k0rVqxQS0uLu9/W64c7MaM0bdo0xcXFjSjTnp6eEQWLP0lJSVFBQYE++OADr5cyJl1+5xbXVGymT5+uWbNmTbjrau3atXrttdf05ptvaubMme52rqP/9WXn6Gom2nWUmJiou+66S/PmzVN9fb3mzp2rn/70p9ZfP0TMKCUmJqqwsFDNzc1R25ubm1VSUuLRqsa2cDisM2fOaPr06V4vZUzKzc2V3++PuqYikYhaWlq4pq7hs88+U1dX14S5rowxeuyxx/Tqq6/qjTfeUG5ubtR+rqPrn6OrmWjX0ZWMMQqHw/ZfP569pNhCjY2NJiEhwbz00kvm9OnTprq62qSkpJj/+q//8nppY0JNTY156623zEcffWRaW1tNeXm5SU1NndDnp7+/37zzzjvmnXfeMZLM1q1bzTvvvGM+/vhjY4wxmzdvNo7jmFdffdV0dHSY73//+2b69Ommr6/P45V/fa51jvr7+01NTY05fPiw6ezsNG+++aYpLi42M2bMmDDn6Ec/+pFxHMe89dZbpru72318/vnn7sxEv46ud44m+nW0adMm8/bbb5vOzk7z7rvvmh//+Mdm0qRJ5sCBA8YYu68fIiZGP//5z82sWbNMYmKi+Yu/+Iuot/BNdA8++KCZPn26SUhIMNnZ2WbZsmXm1KlTXi/LU2+++aaRNOKxYsUKY8yf3h775JNPGr/fb5KSkszf/u3fmo6ODm8X/TW71jn6/PPPTVlZmbnjjjtMQkKCufPOO82KFSvMJ5984vWyvzZXOzeSzC9+8Qt3ZqJfR9c7RxP9OvqHf/gH9/vWHXfcYRYuXOgGjDF2Xz8+Y4z5+u77AAAA3Bq8JgYAAFiJiAEAAFYiYgAAgJWIGAAAYCUiBgAAWImIAQAAViJiAACAlYgYAABgJSIGAABYiYgBAABWImIAAICViBgAAGCl/wGIhIs6YBzMlAAAAABJRU5ErkJggg=="
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "len_peaks = list(map(len,r_peaks))\n",
    "counts, bins = np.histogram(len_peaks)\n",
    "plt.stairs(counts, bins)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T18:55:25.628961248Z",
     "start_time": "2023-08-09T18:55:25.423544349Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "edf_filenames = get_edf_filenames()\n",
    "# choosing 3rd file from my edfs (CX230050.edf)\n",
    "edf_fname = edf_filenames[2]\n",
    "edf_data = import_ecg(edf_fname)\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# shortening the data\n",
    "y = edf_data[0][0].T\n",
    "x = edf_data[0][1]\n",
    "start = 2*int(1e5)\n",
    "length = 10000 #int(2**14)-3\n",
    "y = y[start:start+length]\n",
    "plt.plot(np.linspace(0,len(y)/500, len(y)),y)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "def plot_with_peaks(y, r_peaks, detector_name='nodetect', sampling_freq =500):\n",
    "    # convert sample to nr to time\n",
    "    r_ts = np.array(r_peaks) / sampling_freq\n",
    "    # plotting\n",
    "    plt.figure()\n",
    "    t = np.linspace(0, len(y)/sampling_freq, len(y))\n",
    "    plt.plot(t, y)\n",
    "    plt.plot(r_ts, y[r_peaks], 'ro')\n",
    "    plt.title(f\"{detector_name}\")\n",
    "    plt.ylabel(\"ECG/mV\")\n",
    "    plt.xlabel(\"time/sec\")\n",
    "    plt.show()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "sampling_freq = int(edf_data.info[\"sfreq\"])\n",
    "detectors = Detectors(sampling_freq)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "temp = -1\n",
    "selected_detector = temp\n",
    "\n",
    "if selected_detector < 0:\n",
    "    r_peaks = detectors.engzee_detector(all_data[1]+all_data[2])\n",
    "else:\n",
    "    r_peaks = detectors.get_detector_list()[selected_detector][1](y)\n",
    "plot_with_peaks(y, r_peaks, temp)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "r_peaks = detectors.swt_detector(y)\n",
    "plot_with_peaks(y, r_peaks, temp)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "detector_names = [d[0] for d in detectors.get_detector_list()]\n",
    "detector_names"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "terrible_implementation = [d[0] for d in detectors.get_detector_list()]\n",
    "for i in [0,3,4]:\n",
    "    r_peaks = detectors.get_detector_list()[i][1](y.T)\n",
    "    plot_with_peaks(y, r_peaks,detectors.get_detector_list()[i][0])\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "for i in[0,3,4,5,6,7]:\n",
    "    r_peaks = detectors.get_detector_list()[i][1](y)\n",
    "    plot_with_peaks(y, r_peaks,detectors.get_detector_list()[i][0])"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "r_peaks = detectors.hamilton_detector(y.tolist())\n",
    "plot_with_peaks(y, r_peaks, temp)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "r_peaks = detectors.pan_tompkins_detector(y.T.tolist())\n",
    "plot_with_peaks(y, r_peaks, temp)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "r_peaks"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "qrs_250Hz = [\n",
    "    -7.645702713191108752e-05, -7.106347966654649338e-05,\n",
    "    -6.878923855084972288e-05, -7.376223955418129054e-05,\n",
    "    -7.505940739257362907e-05, -7.359340063839676460e-05,\n",
    "    -7.725458974933605244e-05, -7.940956033719324199e-05,\n",
    "    -7.336339268636284648e-05, -7.456628708858126470e-05,\n",
    "    -7.566086758609246300e-05, -7.349735939169964598e-05,\n",
    "    -5.347989517407805986e-05, -2.576322276495676022e-05,\n",
    "    9.591117385409254366e-06, 1.107073389637588853e-04,\n",
    "    2.961803068709630482e-04, 4.753696858843639616e-04,\n",
    "    6.228405036002417419e-04, 6.402785081510430380e-04,\n",
    "    5.309927524565301229e-04, 3.431126173284082992e-04,\n",
    "    1.764442858986930933e-05, -2.552819441634456661e-04,\n",
    "    -3.073862523701238639e-04, -2.863456790664682282e-04,\n",
    "    -2.518721694690555322e-04, -1.592148022053965631e-04,\n",
    "    -9.424790245873809053e-05, -8.625069025906928586e-05,\n",
    "    -1.135247594299841145e-04, -1.258153399732585710e-04,\n",
    "    -1.193992999231953422e-04, -1.120866635229808870e-04,\n",
    "    -1.134316910065725782e-04, -1.240209251568815188e-04,\n",
    "    -1.259559380186601649e-04, -1.203450016337975629e-04,\n",
    "    -1.110543536387444114e-04, -1.127605818197161149e-04,\n",
    "    -1.170437211404296360e-04, -1.164120454904625621e-04,\n",
    "    -1.114568819107143765e-04, -1.045314027530635311e-04\n",
    "]\n",
    "\n",
    "qrs_360Hz = [\n",
    "    -2.012093043565478800e-01, -1.997445779689931422e-01,\n",
    "    -1.933817485967479488e-01, -1.866300438888101043e-01,\n",
    "    -1.900197917130422021e-01, -1.952489409119362584e-01,\n",
    "    -2.077327660900527839e-01, -2.151062251971897488e-01,\n",
    "    -2.286944266031576134e-01, -2.613282425072631088e-01,\n",
    "    -3.109667077596850460e-01, -3.268329535815900355e-01,\n",
    "    -3.207679356378619340e-01, -2.789804314489847115e-01,\n",
    "    -2.002973727171682705e-01, -7.535273835744371596e-02,\n",
    "    1.617855432133499249e-01, 4.830501280448596946e-01,\n",
    "    8.046776909827563218e-01, 1.110971403512063693e+00,\n",
    "    1.398619676476005047e+00, 1.646910048118821468e+00,\n",
    "    1.795931835917133679e+00, 1.853719993357946683e+00,\n",
    "    1.766209570127352135e+00, 1.507327784812496185e+00,\n",
    "    1.102571692453119923e+00, 6.726173041414026388e-01,\n",
    "    2.887363696891277476e-01, -3.219198191555563970e-02,\n",
    "    -2.403461836981684585e-01, -3.541368003766390826e-01,\n",
    "    -3.791183904479392131e-01, -3.276287305258123728e-01,\n",
    "    -2.194183275717616899e-01, -1.273112649166615873e-01,\n",
    "    -1.028728718059972597e-01, -1.320473557997807190e-01,\n",
    "    -1.689497023598502679e-01, -1.903472688187088646e-01,\n",
    "    -2.038415630949019508e-01, -1.977016005429313439e-01,\n",
    "    -1.964550042183527689e-01, -1.830856541608651133e-01,\n",
    "    -1.880574388507596506e-01, -2.047347501639194822e-01,\n",
    "    -2.272093875641389704e-01, -2.399271748943634652e-01,\n",
    "    -2.414975147279495027e-01, -2.491120256393957733e-01,\n",
    "    -2.286999849340090596e-01\n",
    "]\n",
    "plt.plot(qrs_250Hz)\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "qrs_500hz_linsp = np.linspace(-0.7,0.7,30)\n",
    "qrs_500hz  = -qrs_500hz_linsp**2 + 0.18\n",
    "t = np.linspace(0, len(qrs_500hz) / 500, len(qrs_500hz))\n",
    "plt.plot(t, qrs_500hz)\n",
    "plt.savefig('../Images/R_peaks_detection')"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "t = np.linspace(0, len(qrs_360Hz) / sampling_freq, len(qrs_360Hz))\n",
    "plt.plot(t,qrs_360Hz)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "yy = np.reshape(y, y.size)\n",
    "print(y.shape)\n",
    "yy.shape"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "qrs_500hz_fft = np.fft.fft(qrs_500hz)\n",
    "plt.plot(qrs_500hz_fft)\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "np.sum(qrs_500hz)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "plt.plot(y[12000:15000])"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "conv_500 = np.convolve(yy, qrs_500hz, 'same')\n",
    "print(len(conv_500))\n",
    "plt.plot(conv_500[14000:15000])\n",
    "#plt.plot(np.sqrt(conv_500**2)[12000:15000])\n",
    "plt.plot(y[14000:15000])"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from scipy import interpolate\n",
    "qrs_500Hz_doubled = [qrs_250Hz[i//2] for i in range(2*len(qrs_250Hz))]\n",
    "lin_old = np.linspace(1,len(qrs_250Hz),len(qrs_250Hz))\n",
    "lin_new = np.linspace(1,len(qrs_250Hz), 2*len(qrs_250Hz))\n",
    "qrs_500Hz_interpol = np.interp(lin_new, lin_old, np.array(qrs_250Hz))\n",
    "plt.plot(qrs_500Hz_interpol)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "np.savetxt('qrs_500Hz_interpol', qrs_500Hz_interpol)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "r_peaks = detectors.matched_filter_detector(y, 'qrs_500Hz_interpol')\n",
    "plot_with_peaks(y, r_peaks, temp)"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
